<template>
  <div>
    <el-card shadow="never" class="card_v">
      <el-tabs class="v_tabs" v-model="activeName" @tab-click="handleClick" :stretch="true" v-if="data">
        <el-tab-pane label="基本" name="basic">
          <div class="infoBox">
            <div class="infoTitle">
              <div class="title">背调报告</div>
              <div class="infoTime">
                <div class="time">
                  生成时间：{{
                    companyInfoIndustry && companyInfoIndustry.update_time
                    ? companyInfoIndustry.update_time
                    : "暂无"
                  }}
                </div>
                <div>
                  <el-button v-if="!companyInfoIndustry.company_id" @click="reportLoading" class="item_1" size="mini"
                    type="primary">马上生成报告</el-button>

                  <el-button
                    v-if="companyInfoIndustry && (companyInfoIndustry.status == 2 || companyInfoIndustry.status == 3)"
                    class="item_1" size="mini" disabled type="info">报告生成中，请稍后刷新查看</el-button>

                  <el-button v-if="companyInfoIndustry &&
                    companyInfoIndustry.status &&
                    companyInfoIndustry.status != 2
                    " @click="reportLoading" class="item_1" size="mini" type="primary">重新生成报告</el-button>
                </div>
              </div>
            </div>
            <div style="display: flex; justify-content: flex-end; margin-bottom: 10px">
              <el-button type="primary" class="item_b" size="mini" @click="share">导出</el-button>
            </div>
          </div>
          <el-tabs :before-leave="beforeLeave" v-model="activeNameInfo" type="card">
            <el-tab-pane label="工商信息" name="info">
              <div v-if="activeNameInfo == 'info'">
                <businessInfo :infodata="infodata" :tokenIfon="tokenIfon" :companyInfos="companyInfos" :data="data"
                  :tags="tags" :type="type" :status_update="status_update" :form_admin="form_admin"
                  :companyInfoIndustry="companyInfoIndustry" @update="update"></businessInfo>
              </div>
            </el-tab-pane>

            <el-tab-pane label="企业荣誉" name="honor">
              <enterpriseHonor :honor_know="honor_know" :form_admin="companyInfos"
                :companyInfoIndustry="companyInfoIndustry" v-if="activeNameInfo == 'honor'"></enterpriseHonor>
            </el-tab-pane>

            <el-tab-pane label="专利分析" name="patent">
              <patent :companyInfoIndustry="companyInfoIndustry" v-if="activeNameInfo == 'patent'"></patent>
            </el-tab-pane>

            <el-tab-pane label="商标分析" name="brand">
              <brand :companyInfoIndustry="companyInfoIndustry" v-if="activeNameInfo == 'brand'"></brand>
            </el-tab-pane>
            <el-tab-pane label="其他知产" name="other">
              <other :companyInfoIndustry="companyInfoIndustry" v-if="activeNameInfo == 'other'"></other>
            </el-tab-pane>
            <el-tab-pane label="知产纠纷" name="dispute">
              <dispute :companyInfoIndustry="companyInfoIndustry" v-if="activeNameInfo == 'dispute'"></dispute>
            </el-tab-pane>
          </el-tabs>
        </el-tab-pane>
        <el-tab-pane label="走访" name="interview">
          <interview v-if="activeName == 'interview'" :company_id="company_id"></interview>
        </el-tab-pane>
        <el-tab-pane label="需求" name="demand">
          <demand v-if="activeName == 'demand'" :company_id="company_id"></demand>
        </el-tab-pane>
        <el-tab-pane label="专利" name="patent">
          <Visitpatent v-if="activeName == 'patent'" :infodata="infodata" :form_admin="companyInfos" :PatentNumber="PatentNumber"
            :company_id="company_id"></Visitpatent>
        </el-tab-pane>

        <el-tab-pane label="商标" name="trademark">
          <trademark v-if="activeName == 'trademark'" :form_admin="companyInfos" :PatentNumber="PatentNumber"></trademark>
        </el-tab-pane>
        <el-tab-pane label="诉讼" name="litigation">
          <litigation v-if="activeName == 'litigation'" :form_admin="companyInfos"></litigation>
        </el-tab-pane>
        <el-tab-pane label="荣誉" name="honor">
          <honor v-if="activeName == 'honor'" :PatentNumber="PatentNumber" :form_admin="companyInfos"></honor>
        </el-tab-pane>
        <el-tab-pane label="问卷" name="questionnaire">
          <questionnaire ref="questionnaire" v-if="activeName == 'questionnaire'" :company_id="company_id">
          </questionnaire>
        </el-tab-pane>
      </el-tabs>
    </el-card>

    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" append-to-body>
      <span>当前报告尚未生成，是否开始生成？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="reportLoading">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  currency,
  tianyanchaphoneCurrency,
  loginInfo,
  companyTianynchaRefresh
} from "@/api/newuser";

import {
  companyInfo,
  companydetail,
  visitRefresh,
  visitHonor,
  backsteppingInfo,
} from "@/api/configApi"
import Visitpatent from "./Visitpatent.vue";
import interview from "./interview.vue";
import litigation from "./litigation.vue";
import trademark from "./trademark.vue";
import businessInfo from "./businessInfo.vue";
import enterpriseHonor from "./enterpriseHonor.vue";
import patent from "./patent.vue";
import brand from "./brand.vue";
import other from "./other.vue";
import dispute from "./dispute.vue";
import demand from "./demand.vue";
import questionnaire from "./questionnaire.vue";
import honor from "./honor.vue";
import { getToken } from "@/utils/auth";
import { log } from "util";

export default {
  components: {
    businessInfo,
    patent,
    enterpriseHonor,
    Visitpatent,
    litigation,
    honor,
    trademark,
    interview,
    demand,
    questionnaire,
    brand,
    other,
    dispute,
  },
  data() {
    return {
      id: this.$route.query.id,
      dialogVisible: false,
      url: "http://open.api.tianyancha.com/services/open/ic/baseinfo/normal",
      tags: [],
      data: {},
      activeName: "basic",
      index: 0,
      getToken: getToken() || sessionStorage.getItem("token"),
      tokenIfon: sessionStorage.getItem("token"),
      page: 1,
      honor_know: {},
      limit: 10,
      infodata: {},
      count: 0,
      companyInfos: {},
      num: null,
      PatentNumber: {},
      labelStyle: {
        fontSize: "14px",
        fontWeight: "bold",
        margin: "5px 0px 0 0",
      },
      contentStyle: {
        fontSize: "14px",
        // fontWeight: 'bold'
        margin: "5px 0px 0 0",
      },
      activeNameInfo: "info",
      companyInfoIndustry: {},
      companyInfoIndustryTypeData: false,
    };
  },
  props: {
    type: {
      type: String,
      default: "web",
    },
    table: {
      type: Boolean,
      default: false,
    },
    company_id: {
      type: Number,
      default: 0,
    },
    form_admin: {
      type: Object,
      default: () => ({}),
    },
    status_agent: {
      type: Number,
      default: 1,
    },
    status_update: {
      type: Number,
      default: 0,
    },
  },
  watch: {
    status_agent(val) {
      if (val == 0) {
        // console.log('查询');
        if (this.type == "phone") {
          this.tianyanchaphoneCurrency();
        } else {
          this.currency();
        }
      } else {
        if (this.companyInfos.company) {
          companydetail({ company: this.companyInfos.company }).then((res) => {
            this.data = res;
            this.tags = this.data.tags.split(";");
          });
        }
      }
    },
    company_id: {
      handler(newValue) {
        this.backsteppingInfo();
        this.visitHonor();
        // console.log(val, "公司id");
        companyInfo({
          company_id: this.company_id,
        }).then((res) => {
          console.log(res, "infodata");
          this.infodata = res;
          this.companyInfos = res.res;
          this.companyInfos.company_end = res.company_end
          if (this.companyInfos.company) {
            companydetail({ company: this.companyInfos.company }).then((res) => {
              if (res) {
                this.data = res;
                this.tags = this.data.tags.split(";");
              } else {
                this.currency();
              }
            });
          }
        });
      },
      immediate: true
    },
  },

  async mounted() {
    //天眼查获取token
    // let res = await loginInfo({
    //   account: "shunqi",
    //   password: "123456",
    //   Authorization: this.getToken,
    // });
    // this.tokenIfon = res.token;

    if (this.company_id) {
      this.backsteppingInfo();
      this.visitHonor();
      companyInfo({
        company_id: this.company_id,
      }).then((res) => {

        this.companyInfos = res.res;
        if (this.status_agent == 0) {
          // console.log('查询');
          if (this.type == "phone") {
            this.tianyanchaphoneCurrency();
          } else {
            this.currency();
          }
        } else {
          if (this.companyInfos.company) {
            companydetail({ company: this.companyInfos.company }).then((res) => {
              if (res) {
                this.data = res;
                this.tags = this.data.tags.split(";");
              } else {
                this.currency();
              }
            });
          }
        }
      });
    }
    if (this.companyInfos.company) {
      companydetail({ company: this.companyInfos.company }).then((res) => {
        if (res) {
          this.data = res;
          this.tags = this.data.tags.split(";");
        } else {
          this.currency();
        }
      });
    }
  },
  filters: {
    //时间戳转换
    time(date) {
      date = new Date(date);
      var y = date.getFullYear();
      var m = date.getMonth() + 1;
      m = m < 10 ? "0" + m : m;
      var d = date.getDate();
      d = d < 10 ? "0" + d : d;
      return y + "-" + m + "-" + d;
    },
  },
  beforeDestroy() {
    sessionStorage.removeItem("currencyIndo");
  },
  methods: {
    visitHonor() {
      visitHonor({
        company_id: this.company_id,
      }).then((res) => {
        this.honor_know = res;
      });
    },
    beforeLeave(activeName, oldActiveName) {

      if (this.companyInfoIndustryTypeData) {
        if (this.companyInfoIndustry.update_time) {
          return true;
        } else {
          this.$message.error("报告生成中，请稍等几分钟后刷新查看。");
          return false;
        }
      } else {
        this.dialogVisible = true;
        return false;
      }
    },
    reportLoading() {
      visitRefresh({
        company_id: this.company_id,
      })
        .then((res) => {
          // this.companyInfos = res;
          this.$message({
            message: "生成中",
            type: "success",
          });
          this.dialogVisible = false
          this.backsteppingInfo();
        })
        .catch((res) => {
          this.$message.error("一周内不可再刷新");
          // this.backsteppingInfo();
        });
    },
    share() {
      let routerJump = this.$router.resolve({
        path: "/Backstepping",
        query: {
          company_id: this.company_id,
        },
      });
      window.open(routerJump.href, "_blank");
    },
    backsteppingInfo() {
      backsteppingInfo({
        company_id: this.company_id,
      }).then((res) => {
        this.companyInfoIndustry = res;
        this.companyInfoIndustryTypeData = true;

        let arr = [];
        this.companyInfoIndustry.patent_trend.data[7].map((res) => {
          let demo = res.split("|||");
          arr.push(demo);
        });

        this.companyInfoIndustry.patent_trend.data[7] = arr;

      });
    },

    update() {
      if (this.status_update == 1) {
        if (this.type == "phone") {
          this.tianyanchaphoneCurrency();
          //手机端没有token验证
        } else {
          this.currency();
        }
        // this.$emit('update')
       
      } else {
        this.$message.error("已是最新信息");
      }
    },
    handleClick(e) {
    },
    handleClose() {
      this.$emit("close");
    },
    companyDetails(id) {
      this.$router.push({
        path: "/companyDetails",
        query: {
          company_id: id,
        },
      });
    },
    currency() {
      companyTianynchaRefresh({
        company_id:this.companyInfos.id
      }).then((res) => {
        console.log(res,'天眼查刷新');
        this.$emit("update");
        companyInfo({
          company_id: this.company_id,
        }).then((res) => {
          this.companyInfos = res.res;
        });
      });
    },
    tianyanchaphoneCurrency() {
      tianyanchaphoneCurrency({
        url: this.url,
        params: JSON.stringify({
          keyword: this.form_admin.company_name,
        }),
        inter_id: 1116,
        token: this.tokenIfon,
      }).then((res) => {
        companyInfo({
          company_id: this.company_id,
        }).then((res) => {
          this.companyInfos = res.res;
        });
        // this.data = res;
        // this.tags = this.data.tags.split(";");
        this.$emit("update");
      });
    },
  },
};
</script>

<style scoped lang="less">
.infoBox {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
  position: relative;

  .infoTitle {
    .title {
      font-size: 32px;
      font-weight: bold;
      margin-bottom: 5px;
    }

    .infoTime {
      display: flex;
      align-items: center;

      .time {
        margin-right: 10px;
      }
    }
  }
}

.topBox {
  border-bottom: 3px solid #f4f5f7;
  margin-bottom: 20px;
}

.company_title {
  font-weight: bolder;
  font-size: 24px;
  margin-bottom: 5px;
  position: relative;

  .el-button {
    position: absolute;
    margin-left: 10px;
  }
}

.descriptions_title {
  font-weight: bold;
}

::v-deep .el-drawer__header {
  margin-bottom: 20px;
  display: none;
}

::v-deep .item_b {
  padding: 5px 10px;
  position: absolute;
  right: 25px;
}

::v-deep .item_1 {
  padding: 5px 10px;
  right: 25px;
}

::v-deep .card_v .el-card__body {
  min-height: 1010px;
  padding: 0;

  .el-tabs .el-tabs__content {
    padding: 0 20px;

    tr th {
      font-size: 16px;
    }

    tr td {
      font-size: 16px;
    }
  }
}

.b_red {
  color: #ff5f58 !important;
  background-color: #fff6f5 !important;
}

.b_red2 {
  color: #ffffff !important;
  background-color: #ff5f58 !important;
}

.b_color {
  color: #109421 !important;
  background-color: #c2f9c9 !important;
}

.trademark {
  padding: 12px 0;

  .logo {}

  ._text {
    height: 105px;

    .el-row {
      height: 100%;
      display: flex;
      justify-content: space-between;
      flex-direction: column;

      .title_t {
        display: flex;
        justify-content: space-between;
      }

      .title_b {
        font-size: 12px;
      }
    }
  }
}

.b_bule {
  display: inline-block;
  background-color: #d3e2ff;
  margin: 3px 5px;
  border-radius: 5px;
  padding: 0px 10px;
  color: #387dff;
  font-size: 14px;
  height: 20px;
  line-height: 20px;
  cursor: pointer;
  border: none;
}

// ::v-deep .v_tabs .el-tabs__header .el-tabs__nav .el-tabs__item {
//   padding: 0 20px !important;
// }
.pagination {
  margin-top: 20px;
}

@media (max-width: 1360px) {}

@media (max-width: 1280px) {}

@media (max-width: 768px) {}
</style>
